<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Struts2 | Cying</title><meta name="keywords" content="JavaEE"><meta name="author" content="Cying-承影"><meta name="copyright" content="Cying-承影"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Struts    一、Struts2 概述  应用于 JavaEE 三层结构中 web 层框架 Struts2 框架是在 Struts1 和 WebWork 基础之上发展的全新框架 有一个过滤器，根据不同的操作，在类里面写不同的方法   1.1 - 简单案例1.1.1 - 引入 Jar 包  asm-7.1.jar asm-commons-7.1.jar asm-tree-7.1.jar com">
<meta property="og:type" content="article">
<meta property="og:title" content="Struts2">
<meta property="og:url" content="http://cyingk.github.io/2020/02/01/Struts2/index.html">
<meta property="og:site_name" content="Cying">
<meta property="og:description" content="Struts    一、Struts2 概述  应用于 JavaEE 三层结构中 web 层框架 Struts2 框架是在 Struts1 和 WebWork 基础之上发展的全新框架 有一个过滤器，根据不同的操作，在类里面写不同的方法   1.1 - 简单案例1.1.1 - 引入 Jar 包  asm-7.1.jar asm-commons-7.1.jar asm-tree-7.1.jar com">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2597327423,2034793762&fm=26&gp=0.jpg">
<meta property="article:published_time" content="2020-01-31T17:27:42.000Z">
<meta property="article:modified_time" content="2020-07-22T03:41:53.000Z">
<meta property="article:author" content="Cying-承影">
<meta property="article:tag" content="JavaEE">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2597327423,2034793762&fm=26&gp=0.jpg"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="http://cyingk.github.io/2020/02/01/Struts2/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//www.clarity.ms"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>(function(c,l,a,r,i,t,y){
    c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
    t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
    y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "4zndahq92q");</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"Traditional Chinese Activated Manually","cht_to_chs":"Simplified Chinese Activated Manually","day_to_night":"Dark Mode Activated Manually","night_to_day":"Light Mode Activated Manually","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: false
};

const saveToLocal = {
  // ttl 單位是 天
  set: function setWithExpiry(key, value, ttl) {
    if (ttl === 0) return
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}

// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
const getScript = url => new Promise((resolve, reject) => {
  const script = document.createElement('script')
  script.src = url
  script.async = true
  script.onerror = reject
  script.onload = script.onreadystatechange = function() {
    const loadState = this.readyState
    if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
    script.onload = script.onreadystatechange = null
    resolve()
  }
  document.head.appendChild(script)
})</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-07-22 11:41:53'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = '1'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}const fontSizeVal = saveToLocal.get('global-font-size')
if (fontSizeVal !== undefined) {
  document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
}})()</script><meta name="generator" content="Hexo 5.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">3</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://5b0988e595225.cdn.sohucs.com/images/20190831/84729f5c5bb346208416445feb485df6.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Cying</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Struts2</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2020-01-31T17:27:42.000Z" title="Created 2020-02-01 01:27:42">2020-02-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2020-07-22T03:41:53.000Z" title="Updated 2020-07-22 11:41:53">2020-07-22</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Note/">Note</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">2.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>9min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><html><head></head><body><h1 style="
           font-size: 60px;
           font-weight: bolder;
           border: none;
           "><center>Struts</center></h1>



<h1 id="一、Struts2-概述"><a href="#一、Struts2-概述" class="headerlink" title="一、Struts2 概述"></a>一、Struts2 概述</h1><blockquote>
<ul>
<li>应用于 <code>JavaEE</code> 三层结构中 <code>web</code> 层框架</li>
<li><code>Struts2</code> 框架是在 <code>Struts1</code> 和 <code>WebWork</code> 基础之上发展的全新框架</li>
<li>有一个过滤器，根据不同的操作，在类里面写不同的方法</li>
</ul>
</blockquote>
<h2 id="1-1-简单案例"><a href="#1-1-简单案例" class="headerlink" title="1.1 - 简单案例"></a>1.1 - 简单案例</h2><h3 id="1-1-1-引入-Jar-包"><a href="#1-1-1-引入-Jar-包" class="headerlink" title="1.1.1 - 引入 Jar 包"></a>1.1.1 - 引入 Jar 包</h3><blockquote>
<ul>
<li><code>asm-7.1.jar</code></li>
<li><code>asm-commons-7.1.jar</code></li>
<li><code>asm-tree-7.1.jar</code></li>
<li><code>commons-fileupload-1.4.jar</code></li>
<li><code>commons-io-2.6.jar</code></li>
<li><code>commons-lang3-3.8.1.jar</code></li>
<li><code>freemarker-2.3.28.jar</code></li>
<li><code>javassist-3.20.0-GA.jar</code></li>
<li><code>log4j-api-2.12.1.jar</code></li>
<li><code>log4j-core-2.12.1.jar</code></li>
<li><code>ognl-3.1.26.jar</code></li>
<li><code>struts2-core-2.5.22.jar</code></li>
</ul>
</blockquote>
<h3 id="1-1-2-新建-struts-xml"><a href="#1-1-2-新建-struts-xml" class="headerlink" title="1.1.2 - 新建 struts.xml"></a>1.1.2 - 新建 struts.xml</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!--?xml version="1.0" encoding="UTF-8" ?--></span></span><br><span class="line"><span class="meta">struts</span> <span class="meta-keyword">PUBLIC</span></span><br><span class="line"><span class="meta">  <span class="meta-string">"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">"http://struts.apache.org/dtds/struts-2.5.dtd"</span>></span></span><br><span class="line"></span><br><span class="line">- struts.xml 文件位置固定在 src 文件夹</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-1-3-配置-action"><a href="#1-1-3-配置-action" class="headerlink" title="1.1.3 - 配置 action"></a>1.1.3 - 配置 action</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">struts</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">package</span> <span class="attr">name</span>=<span class="string">"···"</span> <span class="attr">extends</span>=<span class="string">"struts-default"</span> <span class="attr">namespace</span>=<span class="string">"/···"</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">action</span> <span class="attr">name</span>=<span class="string">"···"</span> <span class="attr">class</span>=<span class="string">"Java 文件全路径"</span>></span></span><br><span class="line">      <span class="tag"><<span class="name">result</span> <span class="attr">name</span>=<span class="string">"ok"</span>></span><span class="tag">< /<span class="attr">result</span>></span></span><br><span class="line">    <span class="tag">< /<span class="attr">action</span>></span></span><br><span class="line">  <span class="tag">< /<span class="attr">package</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">struts</span>></span></span><br><span class="line"></span><br><span class="line">- 在 struts.xml 文件中进行</span><br><span class="line">- 访问的基本路径相同的 action 放在同一个 package 中</span><br><span class="line">- package 的 extends 属性是固定的 struts-default</span><br><span class="line">- 外部的访问路径由 package 的 namespace 和 action 的 name 构成</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-1-4-配置-filter"><a href="#1-1-4-配置-filter" class="headerlink" title="1.1.4 - 配置 filter"></a>1.1.4 - 配置 filter</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">  <span class="tag"><<span class="name">filter</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">filter-name</span>></span>struts2<span class="tag">< /<span class="attr">filter-name</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">filter-class</span>></span>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter<span class="tag">< /<span class="attr">filter-class</span>></span></span><br><span class="line">  <span class="tag">< /<span class="attr">filter</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">filter-mapping</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">filter-name</span>></span>struts2<span class="tag">< /<span class="attr">filter-name</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">url-pattern</span>></span>/*<span class="tag">< /<span class="attr">url-pattern</span>></span></span><br><span class="line">  <span class="tag">< /<span class="attr">filter-mapping</span>></span></span><br><span class="line"></span><br><span class="line">- 在 web.xml 文件中进行</span><br><span class="line">- filter 中的类容固定</span><br><span class="line">- filter-mapping 中配置要用到的 filter 和 要处理的路径</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="1-1-5-书写-Java-文件"><a href="#1-1-5-书写-Java-文件" class="headerlink" title="1.1.5 - 书写 Java 文件"></a>1.1.5 - 书写 Java 文件</h3><blockquote>
<p><code>Java</code> 文件要重写以下方法</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> 返回类型 execute() {</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h2 id="1-2-过滤器"><a href="#1-2-过滤器" class="headerlink" title="1.2 - 过滤器"></a>1.2 - 过滤器</h2><blockquote>
<ul>
<li><p>外来请求首先会被汇总到过滤器</p>
</li>
<li><p>过滤器在服务器启动时创建，执行 <code>init</code> 方法</p>
</li>
<li><p>过滤器要做的事</p>
<ul>
<li><p>过滤器会获取请求路径</p>
</li>
<li><p>截取请求路径中的有效字符串</p>
</li>
<li><p>到 <code>src</code> 下面找到 <code>struts.xml</code> 使用 <code>dom4j</code> 进行解析</p>
</li>
<li><p>用截取到的字符串与 <code>struts.xml</code> 中的 <code>action</code> 进行匹配</p>
</li>
<li><p>如果匹配到对应的 <code>action</code> 则根据其中的全路径，反射创建对应 <code>Java</code> 类</p>
<ul>
<li><p>反射：</p>
</li>
<li><pre><code class="java">Class clazz = Class.forName(<span class="string">"全路径"</span>);
<span class="comment">// 反射创建类</span>
Method m = clazz.getMethod(<span class="string">"execute"</span>);
<span class="comment">// 获取方法</span>
Object obj = m.invoke();
<span class="comment">// 方法执行并获取返回值</span>
<!--￼<span class="number">4</span>-->
</code></pre>
</li>
</ul>
</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">Code</div></div><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></tbody></table></figure></div></li>
<li><p>将返回值与该 <code>action</code> 中的 <code>result</code> 进行匹配</p>
<ul>
<li>如果匹配成功，则跳转到相应页面<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">Code</div></div><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">### 1.2.1 - init()</span><br><span class="line"></span><br><span class="line">加载自己创建的配置文件和 `struts2` 自带配置文件</span><br><span class="line"></span><br><span class="line">### 1.2.2 - doFilter()</span><br><span class="line"></span><br><span class="line">**request = warpRequest(request);**</span><br><span class="line"></span><br><span class="line">增强</span><br><span class="line"></span><br><span class="line">## 1.3 - struts.xml</span><br><span class="line"></span><br><span class="line">### 1.3.1 - <package></package></span><br><span class="line"></span><br><span class="line">类似于代码包，用于区分不同的 `action` ，必须首先写 `package` 标签，在 `package` 标签中才能配置 `action`，在一个 `package` 中可以有多个 `action`</span><br><span class="line"></span><br><span class="line">#### - name</span><br><span class="line"></span><br><span class="line">和功能本身没有任何关系，只要重复，可以随便取</span><br><span class="line"></span><br><span class="line">#### - extends</span><br><span class="line"></span><br><span class="line">属性值是固定的，写了这个属性之后，在 `package` 中配置的 `action` 才具有 `action` 该有的功能</span><br><span class="line"></span><br><span class="line">#### - namespace</span><br><span class="line"></span><br><span class="line">要和 `action` 标签的 `name` 构成访问路径，默认值是 `/`</span><br><span class="line"></span><br><span class="line">### 1.3.2 - <action></action></span><br><span class="line"></span><br><span class="line">配置 `action` 的访问路径及 `Java` 文件的绑定</span><br><span class="line"></span><br><span class="line">#### - name</span><br><span class="line"></span><br><span class="line">要和 `package` 标签的 `name` 构成访问路径</span><br><span class="line"></span><br><span class="line">可以用通配符 ***** 进行匹配</span><br><span class="line"></span><br><span class="line">#### - class</span><br><span class="line"></span><br><span class="line">`action` 所绑定 `Java` 文件的全路径</span><br><span class="line"></span><br><span class="line">#### - method</span><br><span class="line"></span><br><span class="line">指定执行 `Java` 文件中的哪一个方法</span><br><span class="line"></span><br><span class="line">在通配符模式下，用 `{1}` 来获取方法，获取到的方法名即 ***** 所代替的字符串</span><br><span class="line"></span><br><span class="line">#### - action 的另外两种编写方式</span><br><span class="line"></span><br><span class="line">- `implements Action`</span><br><span class="line">- `extends ActionSupport`</span><br><span class="line"></span><br><span class="line">### 1.3.3 - <result></result></span><br><span class="line"></span><br><span class="line">根据 `action` 方法返回值，配置到不同的路径</span><br><span class="line"></span><br><span class="line">#### - name</span><br><span class="line"></span><br><span class="line">和 `action` 返回值一样</span><br><span class="line"></span><br><span class="line">#### - type</span><br><span class="line"></span><br><span class="line">指定如何到达配置路径的方法</span><br><span class="line"></span><br><span class="line">- `dispatcher` 请求转发，默认</span><br><span class="line">- `redirect` 重定向</span><br><span class="line">- `chain` 转发到 `action`</span><br><span class="line">- `redirectAction` 重定向到 `Action`</span><br><span class="line"></span><br><span class="line">### 1.3.4 - <constant></constant></span><br><span class="line"></span><br><span class="line">#### - name</span><br><span class="line"></span><br><span class="line">#### - value</span><br><span class="line"></span><br><span class="line">另外两种常量修改方式</span><br><span class="line"></span><br><span class="line">- 在 `src` 下创建 `struts.properties` </span><br><span class="line">- 在 `web.xml` 配置</span><br><span class="line"></span><br><span class="line">### 1.3.5 - <include></include></span><br><span class="line"></span><br><span class="line">####  - file</span><br><span class="line"></span><br><span class="line">文件路径</span><br><span class="line"></span><br><span class="line">### 1.3.6 - <global-results></global-results></span><br><span class="line"></span><br><span class="line">当有多个 `action` 返回值相同、且其 `result` 返回的内容也相同，则可以使用这个</span><br><span class="line"></span><br><span class="line"># 二、数据处理</span><br><span class="line"></span><br><span class="line">## 2.1 - 使用 ActionContext 类</span><br><span class="line"></span><br><span class="line">- 需要创建 `ActionContext` 对象</span><br><span class="line">- 不是 `new` 出来的</span><br><span class="line"></span><br><span class="line">## 2.2 - 使用 ServletActionContext 类</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">## 2.3 - 使用 Interface 注入</span><br><span class="line"></span><br><span class="line">```java</span><br><span class="line">public class A implements ServletRequestAware {</span><br><span class="line"> </span><br><span class="line"> private HttpServletRequest request;</span><br><span class="line"> </span><br><span class="line"> public void setServletRequest(HttpServletRequest request) {</span><br><span class="line">   this.request = request;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line">} </span><br></pre></td></tr></tbody></table></figure></div>
</li>
</ul>
</li>
</ul>
</blockquote>
<h1 id="三、数据封装"><a href="#三、数据封装" class="headerlink" title="三、数据封装"></a>三、数据封装</h1><h2 id="3-1-属性驱动"><a href="#3-1-属性驱动" class="headerlink" title="3.1 - 属性驱动"></a>3.1 - 属性驱动</h2><blockquote>
<ul>
<li>直接把表单提交属性封装到 <code>action</code> 属性</li>
<li>实现步骤<ul>
<li>在 <code>action</code> 成员变量位置定义变量 – 变量名和表单输入项的 <code>name</code> 属性值一样</li>
<li>生成变量的 <code>setter</code> 和 <code>getter</code>  </li>
</ul>
</li>
<li>使用属性封装获取表单数据到属性里面，不能把数据直接封装到实体类</li>
</ul>
</blockquote>
<h2 id="3-2-模型驱动"><a href="#3-2-模型驱动" class="headerlink" title="3.2 - 模型驱动"></a>3.2 - 模型驱动</h2><blockquote>
<ul>
<li><p>使用模型驱动方式，可以直接把表单数据封装到实体类对象里面</p>
</li>
<li><p>实现步骤</p>
<ul>
<li><p><code>action</code> 实现接口 <code>ModelDriven</code> </p>
</li>
<li><p>实现接口里的方法 <code>getModel</code> </p>
</li>
<li><p>在 <code>action</code> 里创建实体类对象</p>
</li>
<li><p>```java<br>public class A extends ActionSupport implements ModelDriven <类型> {</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">Code</div></div><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  private 类型 a = new 类型();</span><br><span class="line"></span><br><span class="line">  public 类型 getModel() {</span><br><span class="line"></span><br><span class="line">    return a;</span><br><span class="line"></span><br><span class="line">  }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div></li>
</ul>
</li>
<li><p>属性驱动和模型封装不能同时使用，如果同时使用，只会执行模型封装</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">Code</div></div><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## 3.3 - 表达式封装</span><br><span class="line"></span><br><span class="line">- 实现过程</span><br><span class="line">  - 使用表达式封装可以把表单数据封装到实体类对象里面</span><br><span class="line">  - 在 `action` 里面声明实体类</span><br><span class="line">  - 生成实体类的 `setter` 和 `getter` 方法</span><br><span class="line"></span><br><span class="line"># 四、Ognl 概述</span><br><span class="line"></span><br><span class="line">- `EL` 表达式：在 `JSP` 中获取域对象里面的值</span><br><span class="line">- `Ognl` 表达式：在 `Struts2` 实现值栈数据</span><br><span class="line">  - 一般把 `Ognl` 在 `Struts2` 里操作，和 `Struts2` 标签一起使用操作值栈</span><br><span class="line">  - 不是 `Struts2` 的一部分，只是经常和 `Struts2` 一起使用</span><br><span class="line"></span><br><span class="line">## 4.1 - 获取值栈对象</span><br><span class="line"></span><br><span class="line">### 4.1.1 - 常用方式</span><br><span class="line"></span><br><span class="line">使用 `ActionContext` 类里面的方法得到值栈对象</span><br><span class="line"></span><br><span class="line">- 获取 `ActionContext` 对象</span><br><span class="line">  - `ActionContext context = ActionContext.getContext();`</span><br><span class="line">- 调用方法得到值栈对象</span><br><span class="line">  - `ValueStack stack = context.getValueStack();`</span><br><span class="line"></span><br><span class="line">## 4.2 - 值栈内部结构</span><br><span class="line"></span><br><span class="line">### 4.2.1 - 第一部分：root</span><br><span class="line"></span><br><span class="line">- 结构是 `List` 集合</span><br><span class="line">- 一般操作的都是 `root` 里的数据</span><br><span class="line"></span><br><span class="line">### 4.2.2 - 第二部分： context</span><br><span class="line"></span><br><span class="line">- 结构是 `Map` 集合</span><br><span class="line">- 很少操作</span><br><span class="line">- 几个键值对</span><br><span class="line">  - `request`：`request` 引用</span><br><span class="line">  - `session`：`HttpSession` 引用</span><br><span class="line">  - `application`： `ServletContext` 引用</span><br><span class="line">  - `parameters`：传递相关参数</span><br><span class="line">  - `attr`： 获取域对象</span><br><span class="line"></span><br><span class="line">## 4.3 - 向值栈放数据</span><br><span class="line"></span><br><span class="line">**调用 set()**</span><br><span class="line"></span><br><span class="line">```java</span><br><span class="line">ActionContext context = ActionContext.getContext();</span><br><span class="line"></span><br><span class="line">ValueStack stack = context.getValueStack();</span><br><span class="line"></span><br><span class="line">stack.set("key", "value"); </span><br></pre></td></tr></tbody></table></figure></div>
</li>
</ul>
</blockquote>
<p><strong>调用 push()</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ActionContext context = ActionContext.getContext();</span><br><span class="line"></span><br><span class="line">ValueStack stack = context.getValueStack();</span><br><span class="line"></span><br><span class="line">stack.put(<span class="string">""</span>);</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>生成 get()</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>.name;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-3-1-向值栈放对象"><a href="#4-3-1-向值栈放对象" class="headerlink" title="4.3.1 - 向值栈放对象"></a>4.3.1 - 向值栈放对象</h3><blockquote>
<ul>
<li>定义对象变量</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> User user = <span class="keyword">new</span> User();</span><br></pre></td></tr></tbody></table></figure></div>

<ul>
<li>生成变量的 <code>get</code> 方法</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">getUser</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> user;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<ul>
<li>在执行的方法里面向对象中设置值</li>
</ul>
</blockquote>
<h3 id="4-3-2-向值栈放-List-对象"><a href="#4-3-2-向值栈放-List-对象" class="headerlink" title="4.3.2 - 向值栈放 List 对象"></a>4.3.2 - 向值栈放 List 对象</h3><blockquote>
<ul>
<li>定义 <code>List</code> 集合</li>
<li>生成变量的 <code>getter</code> </li>
<li>在执行的方法里面向 <code>List</code> 集合设置值</li>
</ul>
</blockquote>
<h2 id="4-4-从值栈获取数据"><a href="#4-4-从值栈获取数据" class="headerlink" title="4.4 - 从值栈获取数据"></a>4.4 - 从值栈获取数据</h2><blockquote>
<ul>
<li>使用 <code>Struts2</code> 的标签 + <code>Ognl</code> 表达式获取值栈数据<ul>
<li><code><s:property value="Ognl 表达式"></s:property></code></li>
</ul>
</li>
</ul>
</blockquote>
<h3 id="4-4-1-获取字符串"><a href="#4-4-1-获取字符串" class="headerlink" title="4.4.1 - 获取字符串"></a>4.4.1 - 获取字符串</h3><p><strong>action</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> <span class="keyword">extends</span> <span class="title">ActionSupport</span> </span>{</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">private</span> String 变量名;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> String get变量名() {</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> 变量名;</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">execute</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line">    </span><br><span class="line">    变量名 = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">return</span> SUCCESS;</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>struts.xml</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">action</span> <span class="attr">name</span>=<span class="string">"自定义名称"</span> <span class="attr">class</span>=<span class="string">"action 全路径"</span>></span></span><br><span class="line"></span><br><span class="line">  <span class="tag"><<span class="name">result</span> <span class="attr">name</span>=<span class="string">"success"</span>></span>/返回的 jsp<span class="tag">< /<span class="attr">result</span>></span></span><br><span class="line">  </span><br><span class="line"><span class="tag">< /<span class="attr">action</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>jsp</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">jsp</div></div><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><s:property value="<span" class="string">"变量名"</s:property></span>/><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-4-2-获取对象"><a href="#4-4-2-获取对象" class="headerlink" title="4.4.2 - 获取对象"></a>4.4.2 - 获取对象</h3><p><strong>action</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> <span class="keyword">extends</span> <span class="title">ActionSupport</span> </span>{</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">private</span> 类名 对象;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> String get对象() {</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> 对象;</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">execute</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line">    </span><br><span class="line">    对象.属性 = <span class="string">"···"</span>;</span><br><span class="line">    ···</span><br><span class="line">    对象.属性 = <span class="string">"···"</span>;</span><br><span class="line">    <span class="keyword">return</span> SUCCESS;</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>struts.xml</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">action</span> <span class="attr">name</span>=<span class="string">"自定义名称"</span> <span class="attr">class</span>=<span class="string">"action 全路径"</span>></span></span><br><span class="line"></span><br><span class="line">  <span class="tag"><<span class="name">result</span> <span class="attr">name</span>=<span class="string">"success"</span>></span>/返回的 jsp<span class="tag">< /<span class="attr">result</span>></span></span><br><span class="line">  </span><br><span class="line"><span class="tag">< /<span class="attr">action</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>jsp</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">jsp</div></div><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><s:property value="<span" class="string">"对象.属性"</s:property></span>/><br><span class="line">···</span><br><span class="line"><s:property value="<span" class="string">"对象.属性"</s:property></span>/><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-4-3-获取-List-对象"><a href="#4-4-3-获取-List-对象" class="headerlink" title="4.4.3 - 获取 List 对象"></a>4.4.3 - 获取 List 对象</h3><p><strong>action</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> <span class="keyword">extends</span> <span class="title">ActionSupport</span></span>{</span><br><span class="line">  </span><br><span class="line">  List<类型> list = <span class="keyword">new</span> List();</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">execute</span><span class="params">()</span> </span>{</span><br><span class="line">    </span><br><span class="line">    类型 对象<span class="number">1</span> = <span class="keyword">new</span> 类型();</span><br><span class="line">    对象<span class="number">1</span>.属性 = <span class="string">"···"</span>;</span><br><span class="line">    ···</span><br><span class="line">    对象<span class="number">1</span>.属性 = <span class="string">"···"</span>;  </span><br><span class="line">    </span><br><span class="line">    类型 对象<span class="number">2</span> = <span class="keyword">new</span> 类型();</span><br><span class="line">    对象<span class="number">2</span>.属性 = <span class="string">"···"</span>;</span><br><span class="line">    ···</span><br><span class="line">    对象<span class="number">2</span>.属性 = <span class="string">"···"</span>;  </span><br><span class="line">    </span><br><span class="line">    list.add(对象<span class="number">1</span>);</span><br><span class="line">    list.add(对象<span class="number">2</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> SUCCESS;</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>struts.xml</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">action</span> <span class="attr">name</span>=<span class="string">"自定义名称"</span> <span class="attr">class</span>=<span class="string">"action 全路径"</span>></span></span><br><span class="line"></span><br><span class="line">  <span class="tag"><<span class="name">result</span> <span class="attr">name</span>=<span class="string">"success"</span>></span>/返回的 jsp<span class="tag">< /<span class="attr">result</span>></span></span><br><span class="line">  </span><br><span class="line"><span class="tag">< /<span class="attr">action</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>jsp</strong></p>
<p><strong>第一种方式</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">jsp</div></div><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><s:property value="<span" class="string">"list[0].属性"</s:property></span>/><br><span class="line">···</span><br><span class="line"><s:property value="<span" class="string">"list[0].属性"</s:property></span>/><br><span class="line"></span><br><span class="line"><s:property value="<span" class="string">"list[1].属性"</s:property></span>/><br><span class="line">···</span><br><span class="line"><s:property value="<span" class="string">"list[1].属性"</s:property></span>/><br></pre></td></tr></tbody></table></figure></div>

<p><strong>第二种方式</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">jsp</div></div><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><s:iterator value="<span" class="string">"list"</s:iterator></span>><br><span class="line">	<s:property value="<span" class="string">"属性"</s:property></span>/><br><span class="line">  ···</span><br><span class="line">	<s:property value="<span" class="string">"属性"</s:property></span>/><br><span class="line">< /s:iterator></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>第三种方式</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang"></div></div><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><s:iterator value="<span" class="string">"list"</s:iterator></span> <span class="keyword">var</span>=<span class="string">"自定义名称"</span>><br><span class="line">	<s:property value="<span" class="string">"#自定义名称.属性"</s:property></span>/><br><span class="line">  ···</span><br><span class="line">	<s:property value="<span" class="string">"#自定义名称.属性"</s:property></span>/><br><span class="line">< /s:iterator></span><br><span class="line">  </span><br><span class="line">- var 会被临时放入 context,需要使用特殊符号 `#` 才能取到</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-4-4-其他操作"><a href="#4-4-4-其他操作" class="headerlink" title="4.4.4 - 其他操作"></a>4.4.4 - 其他操作</h3><h4 id="获取-setter-方法存放的数据"><a href="#获取-setter-方法存放的数据" class="headerlink" title="- 获取 setter 方法存放的数据"></a>- 获取 setter 方法存放的数据</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">jsp</div></div><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><s:property value="<span" class="string">"栈的key"</s:property></span>/><br></pre></td></tr></tbody></table></figure></div>

<h4 id="获取-push-方法存放的数据"><a href="#获取-push-方法存放的数据" class="headerlink" title="- 获取 push 方法存放的数据"></a>- 获取 push 方法存放的数据</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">jsp</div></div><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><s:property value="<span" class="string">"[下标].top"</s:property></span>/><br><span class="line"></span><br><span class="line">- 因为 push 进栈的没有名字，默认存放在 top 数组</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="4-5-两种符号的使用"><a href="#4-5-两种符号的使用" class="headerlink" title="4.5 - 两种符号的使用"></a>4.5 - 两种符号的使用</h2><h3 id="4-5-1"><a href="#4-5-1" class="headerlink" title="4.5.1 -"></a>4.5.1 -</h3><p><strong>先在 action 向域中添加对象</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">execute</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line">  </span><br><span class="line">  HttpServletRequest request = ServletActionContext.getRequest();</span><br><span class="line">  request.setAttribute(key, value);</span><br><span class="line">  <span class="keyword">return</span> SUCCESS;</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>struts.xml</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">action</span> <span class="attr">name</span>=<span class="string">"自定义名称"</span> <span class="attr">class</span>=<span class="string">"action 全路径"</span>></span></span><br><span class="line"></span><br><span class="line">  <span class="tag"><<span class="name">result</span> <span class="attr">name</span>=<span class="string">"success"</span>></span>/返回的 jsp<span class="tag">< /<span class="attr">result</span>></span></span><br><span class="line">  </span><br><span class="line"><span class="tag">< /<span class="attr">action</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>jsp</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">jsp</div></div><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><s:property value="<span" class="string">"#context域的key.域对象名称"</s:property></span>/><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-5-2"><a href="#4-5-2" class="headerlink" title="4.5.2 - %"></a>4.5.2 - %</h3><blockquote>
<p>在 <code>struts2</code> 标签里使用 <code>Ognl</code> 表达式，如果直接在 <code>struts2</code> 表单标签里面使用 <code>Ognl</code> 表达式不识别，只有 % 之后才会识别</p>
</blockquote>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang"></div></div><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><s:textfield name value="%{#context域的key.域对象名称}">< /s:textfield></s:textfield></span><br></pre></td></tr></tbody></table></figure></div>

<h1 id="五、拦截器"><a href="#五、拦截器" class="headerlink" title="五、拦截器"></a>五、拦截器</h1><h2 id="5-1-概述"><a href="#5-1-概述" class="headerlink" title="5.1 - 概述"></a>5.1 - 概述</h2><blockquote>
<ul>
<li><code>struts2</code> 是框架，封装了很多功能，<code>struts2</code> 里面封装的功能都是在拦截器里面</li>
<li><code>struts2</code> 里面封装了很多的功能，又很多拦截器，不是每次这些拦截器都执行，每次执行默认的拦截器</li>
<li>默认拦截器位置：<code>struts2-core-version.jar/struts-default.xml</code></li>
<li>在 <code>action</code> 创建之后，执行之前调用</li>
</ul>
</blockquote>
<h2 id="5-2-底层原理"><a href="#5-2-底层原理" class="headerlink" title="5.2 - 底层原理"></a>5.2 - 底层原理</h2><p><strong>两大思想</strong></p>
<blockquote>
<ul>
<li><code>Aop</code> 思想</li>
<li>职责链设计模式<ul>
<li>要执行多个操作，有添加、修改、删除</li>
<li>首先执行添加操作，添加之后执行修改操作，最后执行删除操作</li>
</ul>
</li>
</ul>
</blockquote>
<p><strong>通过 AOP 将拦截器加入到执行过程</strong></p>
<h2 id="5-3-重要的概念"><a href="#5-3-重要的概念" class="headerlink" title="5.3 - 重要的概念"></a>5.3 - 重要的概念</h2><h3 id="5-3-1-过滤器和拦截器"><a href="#5-3-1-过滤器和拦截器" class="headerlink" title="5.3.1 - 过滤器和拦截器"></a>5.3.1 - 过滤器和拦截器</h3><blockquote>
<ul>
<li>过滤器：理论上可以拦截任意内容</li>
<li>拦截器：只可以拦截 <code>action</code></li>
</ul>
</blockquote>
<h3 id="5-3-2-Servlet-和-action"><a href="#5-3-2-Servlet-和-action" class="headerlink" title="5.3.2 - Servlet 和 action"></a>5.3.2 - Servlet 和 action</h3><blockquote>
<ul>
<li><code>servlet</code> 默认第一次访问时创建，创建一次，单实例对象</li>
<li><code>action</code> 每次访问时创建，创建多次，多实例对象 </li>
</ul>
</blockquote>
<h2 id="5-4-自定义拦截器"><a href="#5-4-自定义拦截器" class="headerlink" title="5.4 - 自定义拦截器"></a>5.4 - 自定义拦截器</h2><h3 id="5-4-1-继承类"><a href="#5-4-1-继承类" class="headerlink" title="5.4.1 - 继承类"></a>5.4.1 - 继承类</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> <span class="keyword">extends</span> <span class="title">AbstractInterceptor</span> </span>{</span><br><span class="line">  </span><br><span class="line">}</span><br><span class="line"><span class="comment">// 而</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AbstractInterceptor</span> <span class="keyword">implements</span> <span class="title">Interceptor</span> </span>{</span><br><span class="line">  </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">- 工作中推荐下面</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> <span class="keyword">extends</span> <span class="title">MethodFilterInterceptor</span> </span>{</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">protected</span> String <span class="title">doIntercept</span><span class="params">(ActionInvocation invocation)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="5-4-2-重写方法"><a href="#5-4-2-重写方法" class="headerlink" title="5.4.2 - 重写方法"></a>5.4.2 - 重写方法</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function">String <span class="title">intercept</span><span class="params">(ActionInvocation invocation)</span></span>;</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="5-4-3-建立关系"><a href="#5-4-3-建立关系" class="headerlink" title="5.4.3 - 建立关系"></a>5.4.3 - 建立关系</h3><blockquote>
<ul>
<li>在要拦截的 <code>action</code> 标签所在的 <code>package</code> 标签里面声明拦截器</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">interceptors</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">interceptor</span> <span class="attr">name</span>=<span class="string">"自定义name"</span> <span class="attr">class</span>=<span class="string">"拦截器java文件全路径"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">interceptors</span>></span></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<ul>
<li>在具体的 <code>action</code> 标签里面使用声明的拦截器</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">interceptor-ref</span> <span class="attr">name</span>=<span class="string">"上面的自定义name"</span>/></span></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<ul>
<li><p>如果在 <code>struts.xml</code> 中使用自定义的，则默认拦截器不会执行</p>
<ul>
<li>解决方法：手动引入默认拦截器</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">interceptor-ref</span> <span class="attr">name</span>=<span class="string">"paramsPrepareParamsStack"</span>/></span></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>
</li>
<li><p>自定义拦截器会对 <code>action</code> 里所有方法都拦截</p>
</li>
</ul>
</blockquote>
<h3 id="5-4-4-放不拦截某些方法"><a href="#5-4-4-放不拦截某些方法" class="headerlink" title="5.4.4 - 放不拦截某些方法"></a>5.4.4 - 放不拦截某些方法</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">interceptor-ref</span> <span class="attr">name</span>=<span class="string">"上面的自定义name"</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">param</span> <span class="attr">name</span>=<span class="string">"excludeMethods"</span>></span>写方法名，多个用逗号隔开<span class="tag">< /<span class="attr">param</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">interceptor-ref</span>></span></span><br></pre></td></tr></tbody></table></figure></div></body></html></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">Cying-承影</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://cyingk.github.io/2020/02/01/Struts2/">http://cyingk.github.io/2020/02/01/Struts2/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaEE/">JavaEE</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/02/07/Vuforia/"><img class="prev-cover" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=775631544,4140499919&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">Vuforia</div></div></a></div><div class="next-post pull-right"><a href="/2020/01/31/Http_Servlet/"><img class="next-cover" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3567041170,3790666556&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">Http Servlet</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2020/08/06/Linux-And-Docker/" title="SpringCloud With Docker"><img class="cover" src="https://img-blog.csdnimg.cn/202008061653587.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-06</div><div class="title">SpringCloud With Docker</div></div></a></div><div><a href="/2020/05/05/SpringCloud/" title="SpringCloud"><img class="cover" src="http://img3.imgtn.bdimg.com/it/u=1799420697,1254911040&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-05</div><div class="title">SpringCloud</div></div></a></div><div><a href="/2020/04/05/SpringBoot/" title="SpringBoot"><img class="cover" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4157804759,1050439081&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-05</div><div class="title">SpringBoot</div></div></a></div><div><a href="/2020/02/20/Mybatis/" title="MyBatis"><img class="cover" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=575314515,4268715674&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-20</div><div class="title">MyBatis</div></div></a></div><div><a href="/2020/02/16/SpringMVC/" title="SpringMVC"><img class="cover" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=790255512,3591398303&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-16</div><div class="title">SpringMVC</div></div></a></div><div><a href="/2020/02/15/Hibernate/" title="Hibernate"><img class="cover" src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=607365457,3589909035&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-15</div><div class="title">Hibernate</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">Struts</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81Struts2-%E6%A6%82%E8%BF%B0"><span class="toc-number">2.</span> <span class="toc-text">一、Struts2 概述</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E7%AE%80%E5%8D%95%E6%A1%88%E4%BE%8B"><span class="toc-number">2.1.</span> <span class="toc-text">1.1 - 简单案例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-1-%E5%BC%95%E5%85%A5-Jar-%E5%8C%85"><span class="toc-number">2.1.1.</span> <span class="toc-text">1.1.1 - 引入 Jar 包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-2-%E6%96%B0%E5%BB%BA-struts-xml"><span class="toc-number">2.1.2.</span> <span class="toc-text">1.1.2 - 新建 struts.xml</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-3-%E9%85%8D%E7%BD%AE-action"><span class="toc-number">2.1.3.</span> <span class="toc-text">1.1.3 - 配置 action</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-4-%E9%85%8D%E7%BD%AE-filter"><span class="toc-number">2.1.4.</span> <span class="toc-text">1.1.4 - 配置 filter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-5-%E4%B9%A6%E5%86%99-Java-%E6%96%87%E4%BB%B6"><span class="toc-number">2.1.5.</span> <span class="toc-text">1.1.5 - 书写 Java 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">2.2.</span> <span class="toc-text">1.2 - 过滤器</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E6%95%B0%E6%8D%AE%E5%B0%81%E8%A3%85"><span class="toc-number">3.</span> <span class="toc-text">三、数据封装</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E5%B1%9E%E6%80%A7%E9%A9%B1%E5%8A%A8"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 - 属性驱动</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E6%A8%A1%E5%9E%8B%E9%A9%B1%E5%8A%A8"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 - 模型驱动</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-1-%E5%90%91%E5%80%BC%E6%A0%88%E6%94%BE%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.2.1.</span> <span class="toc-text">4.3.1 - 向值栈放对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-2-%E5%90%91%E5%80%BC%E6%A0%88%E6%94%BE-List-%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.2.2.</span> <span class="toc-text">4.3.2 - 向值栈放 List 对象</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-%E4%BB%8E%E5%80%BC%E6%A0%88%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="toc-number">3.3.</span> <span class="toc-text">4.4 - 从值栈获取数据</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-1-%E8%8E%B7%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.3.1.</span> <span class="toc-text">4.4.1 - 获取字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-2-%E8%8E%B7%E5%8F%96%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.3.2.</span> <span class="toc-text">4.4.2 - 获取对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-3-%E8%8E%B7%E5%8F%96-List-%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.3.3.</span> <span class="toc-text">4.4.3 - 获取 List 对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-4-%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C"><span class="toc-number">3.3.4.</span> <span class="toc-text">4.4.4 - 其他操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-setter-%E6%96%B9%E6%B3%95%E5%AD%98%E6%94%BE%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-number">3.3.4.1.</span> <span class="toc-text">- 获取 setter 方法存放的数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-push-%E6%96%B9%E6%B3%95%E5%AD%98%E6%94%BE%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-number">3.3.4.2.</span> <span class="toc-text">- 获取 push 方法存放的数据</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-5-%E4%B8%A4%E7%A7%8D%E7%AC%A6%E5%8F%B7%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">3.4.</span> <span class="toc-text">4.5 - 两种符号的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-1"><span class="toc-number">3.4.1.</span> <span class="toc-text">4.5.1 -</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-2"><span class="toc-number">3.4.2.</span> <span class="toc-text">4.5.2 - %</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">4.</span> <span class="toc-text">五、拦截器</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-%E6%A6%82%E8%BF%B0"><span class="toc-number">4.1.</span> <span class="toc-text">5.1 - 概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86"><span class="toc-number">4.2.</span> <span class="toc-text">5.2 - 底层原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-%E9%87%8D%E8%A6%81%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="toc-number">4.3.</span> <span class="toc-text">5.3 - 重要的概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3-1-%E8%BF%87%E6%BB%A4%E5%99%A8%E5%92%8C%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">4.3.1.</span> <span class="toc-text">5.3.1 - 过滤器和拦截器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3-2-Servlet-%E5%92%8C-action"><span class="toc-number">4.3.2.</span> <span class="toc-text">5.3.2 - Servlet 和 action</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-4-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">4.4.</span> <span class="toc-text">5.4 - 自定义拦截器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4-1-%E7%BB%A7%E6%89%BF%E7%B1%BB"><span class="toc-number">4.4.1.</span> <span class="toc-text">5.4.1 - 继承类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4-2-%E9%87%8D%E5%86%99%E6%96%B9%E6%B3%95"><span class="toc-number">4.4.2.</span> <span class="toc-text">5.4.2 - 重写方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4-3-%E5%BB%BA%E7%AB%8B%E5%85%B3%E7%B3%BB"><span class="toc-number">4.4.3.</span> <span class="toc-text">5.4.3 - 建立关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4-4-%E6%94%BE%E4%B8%8D%E6%8B%A6%E6%88%AA%E6%9F%90%E4%BA%9B%E6%96%B9%E6%B3%95"><span class="toc-number">4.4.4.</span> <span class="toc-text">5.4.4 - 放不拦截某些方法</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="footer_custom_text">却道天凉好个秋</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="Increase font size"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="Decrease font size"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>